/*
 *	               _ooOoo_ 
 *	              o8888888o 
 *	              88" . "88 
 *	              (| -_- |) 
 *	              O\  =  /O 
 *	           ____/`---'\____ 
 *	         .'  \\|     |//  `. 
 *	        /  \\|||  :  |||//  \ 
 *	       /  _||||| -:- |||||-  \ 
 *	       |   | \\\  -  /// |   | 
 * 	       | \_|  ''\---/''  |   | 
 * 	       \  .-\__  `-`  ___/-. / 
 * 	    ___ `. .'  /--.--\  `. . __ 
 *	  ."" '<  `.___\_<|>_/___.'  >'"". 
 *	 | | :  `- \`.;`\ _ /`;.`/ - ` : | | 
 *	 \  \ `-.   \_ __\ /__ _/   .-` /  / 
 *======`-.____`-.___\_____/___.-`____.-'====== 
 *                   `=---=' 
 *^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
 *佛祖保佑       永无BUG 
 * File name:          DataToXml.java
 * Copyright@blog.ilvelh.com(China)
 * Editor:           JDK1.7_40
 */
package com.ilvelh.tools;

/**
 * TODO: File comments
 * <p>
 * Author:   ilvelh
 * <p>
 * Date:2014-9-29   Time:下午4:18:46
 * <p>
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.alibaba.fastjson.JSONArray;

/**
 * TODO: File comments
 * <p>
 * Author: Shulei Ling
 * <p>
 * Date:2013-9-5 Time:下午2:39:48
 * <p>
 */
public class DataToXml {

	public static void main(String[] args) throws FileNotFoundException,
			IOException {
		toExport("/users/ilvelh/desktop/data/");
	}

	public static void toExport(String filepath) throws FileNotFoundException,
			IOException {

		File file = new File(filepath);
		if (file.exists() && file.isDirectory()) {
			File[] files = file.listFiles();
			for (File f : files) {
				String name = f.getName();
				System.out.println(name);
				if (!name.endsWith(".dat")) {
					continue;
				}
				name = name.substring(0, name.indexOf(".dat"));
				Document document = DocumentHelper.createDocument(); // 创建文档

				OutputFormat format = OutputFormat.createPrettyPrint();
				format.setEncoding("UTF-8");
				List<String> lines = getLines(new FileInputStream(f));
				Element rootElt = document.addElement("holedata");
				Element holeElt = rootElt.addElement("hole");
				Element teesElt = rootElt.addElement("tees");
				Element tcoorsElt = teesElt.addElement("tcoors");
				Element greenElt = rootElt.addElement("green");
				Element waysElt = rootElt.addElement("ways");
				Element obstaclesElt = rootElt.addElement("obstacles");
				Element flagsElt = rootElt.addElement("flags");
				Element interestsElt = rootElt.addElement("interests");

				// 球洞4点坐标
				Element hole4 = holeElt.addElement("hole4");
				String string0 = lines.get(0);
				string0 = string0.replace("][", "],[");
				StringBuffer sb0 = new StringBuffer(string0);
				sb0.insert(0, "[").append("]");
				JSONArray jarray0 = JSONArray.parseArray(sb0.toString());
				for (Object ja : jarray0) {
					JSONArray j = (JSONArray) ja;
					Element h4coorElt = hole4.addElement("coor");
					h4coorElt.setText(j.getString(0) + "," + j.getString(1));
				}
				// 球洞N点坐标
				Element holeN = holeElt.addElement("holeN");
				String string2 = lines.get(2);
				string2 = string2.replace("][", "],[");
				StringBuffer sb1 = new StringBuffer(string2);
				sb1.insert(0, "[").append("]");
				JSONArray jarray1 = JSONArray.parseArray(sb1.toString());
				for (Object ja : jarray1) {
					JSONArray j = (JSONArray) ja;
					Element hNcoorElt = holeN.addElement("coor");
					hNcoorElt.setText(j.getString(0) + "," + j.getString(1));
				}
				// tee
				String string3 = lines.get(3);
				string3 = string3.replace("][", "],[");
				StringBuffer sb2 = new StringBuffer(string3);
				sb2.insert(0, "[").append("]");
				JSONArray jarray2 = JSONArray.parseArray(sb2.toString());
				String string4 = lines.get(4);
				String[] string4s = string4.split(",");

				for (int i = 0; i < string4s.length; i++) {
					String distance = string4s[i];
					String tname = "未知";
					switch (i) {
					case 0:
						tname = "黑";
						break;
					case 1:
						tname = "金";
						break;
					case 2:
						tname = "蓝";
						break;
					case 3:
						tname = "白";
						break;
					case 4:
						tname = "红";
						break;
					default:
						tname = "未知";
					}
					JSONArray xy = (JSONArray) jarray2.get(i);
					Element teeElt = teesElt.addElement("tee");
					teeElt.addElement("name").setText(tname);
					teeElt.addElement("distance").setText(distance);
					teeElt.addElement("coor").setText(
							xy.getString(0) + "," + xy.getString(1));
					teeElt.addElement("md").setText("00");
					teeElt.addElement("ms").setText("00");
					teeElt.addElement("fd").setText("00");
					teeElt.addElement("fs").setText("00");
				}
				String string1 = lines.get(1);
				string1 = string1.replace("][", "],[");
				StringBuffer sb3 = new StringBuffer(string1);
				sb3.insert(0, "[").append("]");
				JSONArray jarray3 = JSONArray.parseArray(sb3.toString());
				JSONArray frontJarray = (JSONArray) jarray3.get(0);
				JSONArray centerJarray = (JSONArray) jarray3.get(1);
				JSONArray backJarray = (JSONArray) jarray3.get(2);

				greenElt.addElement("front").setText(
						frontJarray.get(0) + "," + frontJarray.get(1));

				Element flag1 = flagsElt.addElement("flag");
				flag1.addElement("name").setText("前");
				flag1.addElement("coor").setText(
						frontJarray.get(0) + "," + frontJarray.get(1));

				greenElt.addElement("center").setText(
						centerJarray.get(0) + "," + centerJarray.get(1));
				Element flag2 = flagsElt.addElement("flag");
				flag2.addElement("name").setText("中");
				flag2.addElement("coor").setText(
						centerJarray.get(0) + "," + centerJarray.get(1));

				greenElt.addElement("back").setText(
						backJarray.get(0) + "," + backJarray.get(1));
				Element flag3 = flagsElt.addElement("flag");
				flag3.addElement("name").setText("后");
				flag3.addElement("coor").setText(
						backJarray.get(0) + "," + backJarray.get(1));

				// green4
				Element green4Elt = greenElt.addElement("green4");

				String string5 = lines.get(5);
				string5 = string5.replace("][", "],[");
				StringBuffer sb4 = new StringBuffer(string5);
				sb4.insert(0, "[").append("]");
				JSONArray jarray4 = JSONArray.parseArray(sb4.toString());
				for (Object ja : jarray4) {
					JSONArray j = (JSONArray) ja;
					Element elt = green4Elt.addElement("coor");
					elt.setText(j.getString(0) + "," + j.getString(1));
				}

				Element greenNElt = greenElt.addElement("greenN");

				// 果岭兴趣点
				String string6 = lines.get(6);
				string6 = string6.replace("][", "],[");
				StringBuffer sb5 = new StringBuffer(string6);
				sb5.insert(0, "[").append("]");
				JSONArray jarray5 = JSONArray.parseArray(sb5.toString());
				for (Object ja : jarray5) {
					JSONArray j = (JSONArray) ja;
					Element elt = interestsElt.addElement("coor");
					elt.setText(j.getString(0) + "," + j.getString(1));
				}
				// 果岭兴趣点
				String string7 = lines.get(7);
				string7 = string7.replace("][", "],[");
				StringBuffer sb6 = new StringBuffer(string7);
				sb6.insert(0, "[").append("]");
				JSONArray jarray6 = JSONArray.parseArray(sb6.toString());
				for (Object ja : jarray6) {
					JSONArray j = (JSONArray) ja;
					Element elt = obstaclesElt.addElement("coor");
					elt.setText(j.getString(0) + "," + j.getString(1));
				}
				// 填充数据
				for (int i = 0; i < 3; i++) {
					// obstaclesElt.addElement("coor").setText("0,0");
					greenNElt.addElement("coor").setText("0,0");
					waysElt.addElement("coor").setText("0,0");
					tcoorsElt.addElement("coor").setText("0,0");
				}

				FileOutputStream fos = new FileOutputStream(filepath + "/"
						+ name + ".xml");
				OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");// 指定编码，防止写中文乱码
				BufferedWriter bw = new BufferedWriter(osw);
				XMLWriter writer = new XMLWriter(bw, format);
				writer.write(document);
				writer.flush();
				writer.close();
				bw.close();
				osw.close();
				fos.close();

			}

		}

	}

	public static List<String> getLines(InputStream is) throws IOException {
		List<String> lines = new ArrayList<String>();
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String temp = null;
		while ((temp = br.readLine()) != null) {
			lines.add(temp);
		}
		return lines;
	}
}
